package com.icen.utils.comm;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.content.Context;

/**
 * Resource Utils Class
 * @author IcenHan
 *
 */
public class ResourceUtils {

	/**
	 * 获取Assets文件夹下的文件的文件内容
	 * 
	 * @param context
	 * @param fileName
	 * @return String
	 */
	public static String geFileContentFromAssets(Context context,
			String fileName) {
		if (context == null || ValidateUtils.isEmpty(fileName)) {
			return null;
		}

		StringBuilder s = new StringBuilder("");
		try {
			InputStreamReader in = new InputStreamReader(context.getResources()
					.getAssets().open(fileName));
			BufferedReader br = new BufferedReader(in);
			String line;
			while ((line = br.readLine()) != null) {
				s.append(line);
			}
			return s.toString();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取Raw文件夹下的文件的文件内容
	 * 
	 * @param context
	 * @param resId
	 * @return String
	 */
	public static String geFileContentFromRaw(Context context, int resId) {
		if (context == null) {
			return null;
		}

		StringBuilder s = new StringBuilder();
		try {
			InputStreamReader in = new InputStreamReader(context.getResources()
					.openRawResource(resId));
			BufferedReader br = new BufferedReader(in);
			String line;
			while ((line = br.readLine()) != null) {
				s.append(line);
			}
			return s.toString();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
}
